package userdao

import (
	"time"
	"wzgames/internal/gdao"
	"wzgames/internal/model/user"
)

type AccountDao gdao.DaoSource

const sqlGetUidByUHash = "select * from user_hash where `uhash`=? and `agtid`=? limit 1"

func (dao *AccountDao) GetUidByUHash(uh string, agtId int64) (*user.UserHashEntity, error) {
	acc := &user.UserHashEntity{}
	err := dao.Get(acc, sqlGetUidByUHash, uh, agtId)
	return acc, err
}

const sqlGetAccountByUid = "select * from user where `uid`=? limit 1"

func (dao *AccountDao) GetAccountByUid(uid uint64) (*user.UserEntity, error) {
	acc := &user.UserEntity{}
	err := dao.Get(acc, sqlGetAccountByUid, uid)
	return acc, err
}

const sqlGetAccountByFlag = "select * from user where `is_robot`=? limit 100"

func (dao *AccountDao) GetAccountByFlag(flag uint64) ([]*user.UserEntity, error) {
	rows, err := dao.Queryx(sqlGetAccountByFlag, flag)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return nil, err
	}
	ret := make([]*user.UserEntity, 0)
	for rows.Next() {
		rd := &user.UserEntity{}
		err = rows.StructScan(rd)
		if err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			return nil, err
		}
		ret = append(ret, rd)
	}
	return ret, err
}

const sqlCreateAccount = "insert into user(`uid`,`uname`,`agtid`,`agtuid`,`status`,`uhead`,`created`,`updated`)" +
	" values(:uid,:uname,:agtid,:agtuid,:status,:uhead,:created,:updated)"

func (dao *AccountDao) CreateAccount(user *user.UserEntity) error {
	_, err := dao.NamedExec(sqlCreateAccount, user)
	return err
}

const sqlCreateUHash = "insert into user_hash(`uhash`,`uid`,`agtid`) values(:uhash,:uid,:agtid)"

func (dao *AccountDao) CreateUHash(uhashE *user.UserHashEntity) error {
	_, err := dao.NamedExec(sqlCreateUHash, uhashE)
	return err
}

const sqlInitUserLatest = "insert into user_latest(`uid`,`agtid`,`created`,`updated`) values(?,?,?,?)"

func (dao *AccountDao) InitUserLatest(uid uint64, agtId int64, t time.Time) error {
	_, err := dao.Exec(sqlInitUserLatest, uid, agtId, t, t)
	return err
}

const sqlUpdateUserLatest = "update user_latest set `instid`=?,`currid`=?,`gameid`=?,`uip`=?,`updated`=? where `uid`=?"

func (dao *AccountDao) UpdateUserLatest(uid uint64, instId int64, currId, gameId int32, ip []byte, t time.Time) (int64, error) {
	result, err := dao.Exec(sqlUpdateUserLatest, instId, currId, gameId, ip, t, uid)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlCreateUserLogin = "insert into user_logins(`uid`,`nodeid`,`ip`,`dateline`,`uname`,`agtid`,`agtuid`) values(:uid,:nodeid,:ip,:dateline,:uname,:agtid,:agtuid)"

func (dao *AccountDao) CreateUserLogin(ent *user.UserLoginEntity) error {
	_, err := dao.NamedExec(sqlCreateUserLogin, ent)
	return err
}

const sqlCreateUserGameLogin = "insert into user_login_games(`uid`,`instid`,`nodeid`,`currid`,`gameid`,`ip`,`dateline`,`uname`,`agtid`,`agtuid`,`device`,`dinfo`)" +
	" values(:uid,:instid,:nodeid,:currid,:gameid,:ip,:dateline,:uname,:agtid,:agtuid,:device,:dinfo)"

func (dao *AccountDao) CreateUserGameLogin(ent *user.UserGameLoginEntity) error {
	_, err := dao.NamedExec(sqlCreateUserGameLogin, ent)
	return err
}
